package com.ck.company.controller;

import com.ck.company.service.CpDeviceInfoService;
import com.ck.company.service.CpProgramService;
import com.ck.constant.DateFormatConstant;
import com.ck.constant.ProgramConstant;
import com.ck.constant.RequestConstant;
import com.ck.exception.ServiceException;
import com.ck.model.common.User;
import com.ck.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author dxy
 * @date 2018/7/12 12:03
 */
@Controller
@RequestMapping("/cpweb")
public class CpWebController {
	private static Logger logger = LoggerFactory.getLogger(CpWebController.class);
	@Autowired
	private CpProgramService cpProgramService;
	@Autowired
	private CpDeviceInfoService cpDeviceInfoService;

	/**
	 * 跳转至商家首页
	 */
	@RequestMapping(value = "/goCpHome")
	public String goCpHome(HttpServletRequest request, Model model) {
		User user = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
		String companyCode = user.getCompanyCode();
		try {
			//实时概况
			long currentTimeMillis = System.currentTimeMillis();
			String currentTime = DateUtils.longTimeStampToStringDate(currentTimeMillis, DateFormatConstant.LONG_DATE_FORMAT_STR);
			model.addAttribute("currentTime", currentTime);
			//节目总数
			Long programCount = cpProgramService.countProgram(companyCode, null);
			model.addAttribute("programCount", programCount);
			//待审核节目数
			Long pendingCount = cpProgramService.countProgram(companyCode, ProgramConstant.PROGRAM_STATUS_APPROVAL_PENDING);
			model.addAttribute("pendingCount", pendingCount);
			//联网终端数
			Long onlineDeviceCount = cpDeviceInfoService.getOnlineDeviceInfoCount(companyCode);
			model.addAttribute("onlineDeviceCount", onlineDeviceCount);
			//离线终端数
			Long offlineDeviceCount = cpDeviceInfoService.getOfflineDeviceInfoCount(companyCode);
			model.addAttribute("offlineDeviceCount", offlineDeviceCount);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("CpWebController goCpHome 跳转至商家首页失败：" + e.getMessage());
		}
		return "cp/cp_home";
	}

}
